package com.san.common.utils;

/**
 * ColumnTypeConversion 字段类型转换工具
 * 
 * @author hulp
 *
 */

// TODO: 类型转换可能有所出入   存在优化空间
public class CTConversionUtils {

	/**
	 * ORACLE TO MYSQL 数据类型转换
	 * 
	 * @param columnType
	 * @param columnLength
	 * @param columnPrecision
	 * @param columnScale
	 * @return
	 */
	public static String O2M(String columnType, Long columnLength, Long columnPrecision, Long columnScale) {
		columnType = StringUtils.upperCase(columnType);
		
		columnScale = (columnScale != null?columnScale:0l);
		
		String result = null;
		switch (columnType) {
		case "CHAR":
			result = "VARCHAR" + "(" + columnLength + ")";
			break;
		case "VARCHAR2":
			if( columnLength >= 1000L) {
				result = "TEXT";
			}else {
				result = "VARCHAR" + "(" + columnLength + ")";
			}
			break;
		case "NCHAR":
			result = "VARCHAR" + "(" + columnLength + ")";
			break;
		case "NVARCHAR2":
			result = "VARCHAR" + "(" + columnLength + ")";
			break;
		case "DATE":
			result = "DATETIME";
			break;
		case "TIMESTAMP":
			result = "DATETIME";
			break;
		case "LONG":
			result = "TEXT";
			break;
		case "RAW":
			result = "BLOB";
			break;
		case "LONG RAW":
			result = "BLOB";
			break;
		case "BLOB":
			result = "BLOB";
			break;
		case "CLOB":
			result = "LONGBLOB";
			break;
		case "NCLOB":
			result = "LONGBLOB";
			break;
		case "NUMBER":
			if (columnScale != 0) {
				result = "DOUBLE" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "BIGINT" + "(" + columnLength + ")";
			}
			break;
		case "DECIMAL":
			result = "DECIMAL" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		case "INTEGER":
			result = "INTEGER" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		case "FLOAT":
			result = "FLOAT" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		case "REAL":
			result = "FLOAT" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		default:
			if (columnType.startsWith("TIMESTAMP")) {
				result = "DATETIME";
			} else {
				result = columnType + "(" + columnLength + ")";
			}
			
			break;
		}

		return result;
	}

	/**
	 * MYSQL TO ORACLE 数据类型转换
	 * 
	 * @param columnType
	 * @param columnLength
	 * @param columnPrecision
	 * @param columnScale
	 * @return
	 */
	public static String M2O(String columnType, Long columnLength, Long columnPrecision, Long columnScale) {
		columnType = StringUtils.upperCase(columnType);
		String result = null;
		switch (columnType) {
		case "VARCHAR":
			result = "VARCHAR2" + "(" + columnLength + ")";
			break;
		case "TINYINT":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "SMALLINT":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "MEDIUMINT":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "INT":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "BIGINT":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "DECIMAL":
			result = "DECIMAL" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		case "FLOAT":
			result = "FLOAT" + "(" + columnPrecision + "," + columnScale + ")";
			break;
		case "DOUBLE":
			if(columnScale == null) {
				result = "NUMBER" + "(" + columnLength + ")";
			}else if (columnScale != 0) {
				result = "NUMBER" + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				result = "NUMBER" + "(" + columnPrecision + ")";
			}
			break;
		case "DATETIME":
			result = "DATE";
			break;
		case "DATE":
			result = "DATE";
			break;
		case "TIMESTAMP":
			result = "DATETIME";
			break;
		case "YEAR":
			result = "NUMBER(3,0)";
			break;
		case "CHAR":
			result = "CHAR" + "(" + columnLength + ")";
			break;
		case "TINYTEXT":
			result = "CLOB";
			break;
		case "TEXT":
			result = "CLOB";
			break;
		case "MEDIUMTEXT":
			result = "CLOB";
			break;
		case "LONGTEXT":
			result = "CLOB";
			break;
		case "TINYBLOB":
			result = "BLOB";
			break;
		case "BLOB":
			result = "BLOB";
			break;
		case "MEDIUMBLOB":
			result = "BLOB";
			break;
		case "LONGBLOB":
			result = "BLOB";
			break;
		case "BINARY":
			result = "RAW" + "(" + columnLength + ")";
			break;
		case "VARBINARY":
			result = "RAW" + "(" + columnLength + ")";
			break;
		default:
			result = columnType + "(" + columnLength + ")";
			break;
		}

		return result;
	}

	/**
	 * 同数据类型 字段信息补全
	 * 
	 * @param columnType
	 * @param columnLength
	 * @param columnPrecision
	 * @param columnScale
	 * @return
	 */
	public static String Complete(String columnType, Long columnLength, Long columnPrecision, Long columnScale) {
		columnType = StringUtils.upperCase(columnType);
		if ("DATE".equals(columnType)) {
			return columnType;
		}
		if ("DATETIME".equals(columnType)) {
			return columnType;
		}
		if (columnPrecision == null) {
			if (columnLength == null) {
				return columnType;
			} else {
				return columnType + "(" + columnLength + ")";
			}

		} else {
			if (columnScale != null) {
				return columnType + "(" + columnPrecision + "," + columnScale + ")";
			} else {
				return columnType + "(" + columnPrecision + ")";
			}
		}
	}
	
	/**
	 * 判断字段类型是否为时间
	 * @param columnType
	 * @return
	 */
	public static final boolean isDateColumn(String columnType) {
		columnType = columnType.toUpperCase();
		if ("DATE".equals(columnType) || "DATETIME".equals(columnType) || "TIMESTAMP".equals(columnType)) {
			return true;
		}
		
		return false;
	}

}
